import requests                                 # 导入http请求模块
import pytest                                   # 导入pytest 测试框架
import allure                                   # 导入allure 测试报告模块
from Common.Random import Pubcs                 # 导入封装的随机文字模块
import time                                     # 导入时间模块

from config import readconfig                   # 获取config配置文件信息中 URL、timeout
Conf = readconfig.Read()                        # 实例化 readconfig.Read 这个类




@pytest.fixture (scope="session")
def KeshiID():                                                     # 定义一个方法  获取医珍通管理员端创建科室后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    YYusername = Conf.get_Hospital("userName")                     # 调用配置的账号
    YYpassword = Conf.get_Hospital("passWord")                     # 调用配置的密码
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    sz = Pubcs.Suiji()                                             # 调用封装的随机数字
    Atime = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime(time.time()))

    # 添加科室
    Canshu = {"hospitalId": YYid,
              "sectionName": "Api添加" + wz1 + wz2 + "科室",
              "linkName": "科室联系人" + wz1 + wz2,
              "linkPhone": "168" + str(sz),
              "sectionCode": "/uploaded/af428a6f-e4c0-483c-89e7-cc81508143db.jpg",
              "sectionDes": 'Api添加：' + wz1 + wz2 + '科室' + '\n添加时间：' + Atime
              }
    c = "Api添加" + wz1 + wz2 + "科室"
    cc = 'Api添加：' + wz1 + wz2 + '科室' + '\n添加时间：' + Atime
    s = requests.post(YYurl + "/yahoo/hosptital/saveSections", json=Canshu, timeout=int(TM))

    # 获取添加的科室ID
    Canshu = {"hospitalId": YYid}
    r = requests.post(YYurl + "/yahoo/hosptital/getSections", json=Canshu, timeout=int(TM))
    a = r.json()["data"]
    for n in range(len(a)):
        if a[n]["sectionName"] == c:
            # print(n)
            # print(r.json()["data"][n]["id"])
            sss = r.json()["data"][n]["id"]
            return sss, cc




@pytest.fixture (scope="session")
def YishengID(KeshiID):                                            # 定义一个方法  获取医珍通管理员端创建医生后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    YYusername = Conf.get_Hospital("userName")                     # 调用配置的账号
    YYpassword = Conf.get_Hospital("passWord")                     # 调用配置的密码
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    sz = Pubcs.Suiji()                                             # 调用封装的随机数字

    # 添加医生
    id, _ = KeshiID
    Canshu = {"hospitalId": YYid,
              "userName": sz,
              "password": sz,
              "trueName": "Api" + wz1 + wz2,
              "sectionId": id}
    s = requests.post(YYurl + "/yahoo/hosptital/addDoctor", json=Canshu, timeout=int(TM))
    c = "Api" + wz1 + wz2

    # 获取添加的医生ID
    Canshu = {"hospitalId": YYid,
              "id": ""}
    r = requests.post(YYurl + "/yahoo/hosptital/getDoctors", json=Canshu, timeout=int(TM))
    a = r.json()["data"]
    for n in range(len(a)):
        if a[n]["trueName"] == c:
            # print(n)
            # print(r.json()["data"][n]["id"])
            sss = r.json()["data"][n]["id"]
            return sss



@pytest.fixture (scope="session")
def YishengID2(KeshiID):                                           # 定义一个方法  获取医珍通管理员端创建医生后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    YYusername = Conf.get_Hospital("userName")                     # 调用配置的账号
    YYpassword = Conf.get_Hospital("passWord")                     # 调用配置的密码
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    sz = Pubcs.Suiji()                                             # 调用封装的随机数字

    # 添加医生
    id, _ = KeshiID
    Canshu = {"hospitalId": YYid,
              "userName": sz,
              "password": sz,
              "trueName": "Api" + wz1 + wz2,
              "sectionId": id}
    s = requests.post(YYurl + "/yahoo/hosptital/addDoctor", json=Canshu, timeout=int(TM))
    c = "Api" + wz1 + wz2

    # 获取添加的医生ID
    Canshu = {"hospitalId": YYid,
              "id": ""}
    r = requests.post(YYurl + "/yahoo/hosptital/getDoctors", json=Canshu, timeout=int(TM))
    a = r.json()["data"]
    for n in range(len(a)):
        if a[n]["trueName"] == c:
            # print(n)
            # print(r.json()["data"][n]["id"])
            sss = r.json()["data"][n]["id"]
            return sss



@pytest.fixture (scope="session")
def Activity():                                                    # 定义一个方法  获取医珍通管理员端创建活动后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    sz = Pubcs.Suiji()                                             # 调用封装的随机数字
    Atime = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime(time.time()))
    # 新增会议型活动
    time_stamp = int(round(time.time() * 1000))
    Canshu = {"title": "Api查看会议型活动《" + wz1 + wz2 + "》",
              "activeMaxPeople": 200,
              "description": "Api新增会议型活动《" + wz1 + wz2 + "》用于【查看】，\n创建时间：" + Atime,
              "hospitalId": YYid,
              "logoImg": "/uploaded/96078a77-72f9-44bd-830e-89128668ab10.png",
              "createTime": time_stamp,
              "sendFlag": "N",
              "activeType": "会议型",
              "activeDate": "2023-08-30"}
    r = requests.post(YYurl + "/yahoo/active/creteActive", json=Canshu, timeout=int(TM))
    c = "Api查看会议型活动《" + wz1 + wz2 + "》"
    xx = "Api新增会议型活动《" + wz1 + wz2 + "》用于【查看】，\n 创建时间：" + Atime
    # 获取新增的会议型活动ID
    Canshu = {"hospitalId": YYid,
              "pageSize": 10,
              "currentPage": 1}
    GET = requests.post(YYurl + "/yahoo/active/getActiveById", json=Canshu, timeout=int(TM))
    a = GET.json()["data"]
    for n in range(len(a)):
        if a[n]["title"] == c:
            # print(n)
            # print(GET.json()["data"][n]["id"])
            HDID = GET.json()["data"][n]["id"]
            return HDID, xx





@pytest.fixture (scope="session")
def Activ():                                                       # 定义一个方法  获取医珍通管理员端创建活动后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    Atime = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime(time.time()))

    # 新增会议型活动
    time_stamp = int(round(time.time() * 1000))
    Canshu = {"title": "Api会议型活动《" + wz1 + wz2 + "》",
              "activeMaxPeople": 200,
              "description": "Api新增会议型活动《" + wz1 + wz2 + "》用于【发布】【删除】，\n创建时间：" + Atime,
              "hospitalId": YYid,
              "logoImg": "/uploaded/96078a77-72f9-44bd-830e-89128668ab10.png",
              "createTime": time_stamp,
              "sendFlag": "N",
              "activeType": "会议型",
              "activeDate": "2023-10-30"}
    r = requests.post(YYurl + "/yahoo/active/creteActive", json=Canshu, timeout=int(TM))
    o = "Api查看会议型活动《" + wz1 + wz2 + "》"

    # 获取新增的会议型活动ID
    Canshu = {"hospitalId": YYid,
              "pageSize": 10,
              "currentPage": 1}
    y = requests.post(YYurl + "/yahoo/active/getActiveById", json=Canshu, timeout=int(TM))
    b = y.json()["data"]
    for n in range(len(b)):
        if b[n]["title"] == o:
            print(n)
            print(y.json()["data"][n]["id"])
    hid = y.json()["data"][n]["id"]
    return hid,o





@pytest.fixture (scope="session")
def Announcement():                                                # 定义一个方法  获取医珍通管理员端创建公告后的ID
    YYurl = Conf.get_Hospital("URL")                               # 调用配置的URL
    YYid = Conf.get_Hospital("hospitalId")                         # 调用配置的医院名称
    TM = Conf.get_Hospital("timeout")                              # 调用配置的超时时间
    wz1 = Pubcs.Unicode()                                          # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()                                          # 调用封装的随机汉字
    Atime = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime(time.time()))

    # 新增公告
    wz1 = Pubcs.Unicode()  # 调用封装的随机汉字
    wz2 = Pubcs.GBK2312()  # 调用封装的随机汉字
    Canshu = {"title": 'API新增《' + wz1 + wz2 + '》公告',
              "noticeDesc": 'API新增《' + wz1 + wz2 + '》公告' + '\n创建时间：' + Atime,
              "hospitalId": YYid,
              "isShow": "Y"}
    r = requests.post(YYurl + "/yahoo/hosptital/saveNotice", json=Canshu, timeout=int(TM))
    oo = 'API新增《' + wz1 + wz2 + '》公告'

    # 获取新增的公告ID
    Canshu = {"hospitalId": YYid,
              "pageSize": 10,
              "currentPage": 1}
    y = requests.post(YYurl + "/yahoo/hosptital/getNoticeListByHospitalId", json=Canshu, timeout=int(TM))
    b = y.json()["data"]
    for n in range(len(b)):
        if b[n]["title"] == oo:
            print(n)
            print(y.json()["data"][n]["id"])
            gid = y.json()["data"][n]["id"]
            return gid, oo
